<template>
  <div>
    <h1>didi</h1>
    {{count}}
    <button @click="byebye">拜拜</button>
  </div>
</template>

<script>
import PubSub from 'pubsub-js'

export default {
    name:'Didi',
    data(){
        return {
            count:0,
            pubId:null
        }
    },
    mounted() {
        // 先订阅，一般在初始化环节就订阅
        this.pubId =PubSub.subscribe('count',(_,count)=>{
            this.count = count
        })
    },
    methods:{
        byebye(){
            // 取消订阅
            //unsubscribe也可接受一个订阅id(pubId),是订阅方法的返回值,可以取消当前组件的当前订阅
            PubSub.unsubscribe(this.pubId)
        }
    }
}
</script>

<style>

</style>